﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;
using System.IO;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;


public partial class RecordsListD2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["name"] == null)
        {
            Response.Redirect("~/error.aspx?");
        }
        else
        {
            if (!IsPostBack)
            {
                this.bind();
            }
        }
    }
    public SqlConnection GetConnection()
    {
        string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection myConn = new SqlConnection(myStr);
        return myConn;
    }

    protected void bind()
    {
            SqlConnection myConn = GetConnection();
            myConn.Open();
            string sqlStr = "select RID as 编号 , RGID as 货品编号,GTYPEName as 类别,GCHName as 名称,GXinhao as 型号,GBrand as 品牌,  RWID as 仓库编号,RQuantity as 数量,RHandler as 收货人,RDate as 日期,RifAOG as 到货数量,RComfirmer as 确认人,RComfirmedDate as 确认日期,bianhao as 项目编号,PName as 项目名  from RecordsIN, Goods,Projects where RecordsIN.RGID=Goods.GID and RecordsIN.RPID=Projects.PID and  RecordsIN.RDID='" + Session["DID"].ToString() + "' order by RID desc";
            SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
            DataSet myDs = new DataSet();
            myDa.Fill(myDs);
            GridView1.DataSource = myDs;
             GridView1.DataKeyNames = new string[] { "编号" };
            GridView1.DataBind();
            myDa.Dispose();
            myDs.Dispose();
            myConn.Close();
    }

    public bool IsNumberic(string str)
    {

        Regex reg = new Regex("^(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*))$");
        Match ma = reg.Match(str);
        if (ma.Success)
        {
            //是数字  
            return true;
        }
        else
        {
            //不是数字    
            return false;
        }
    } 

    protected void Button1_Click(object sender, EventArgs e)
    {
         if (Session["name"] == null)
        {
            Response.Redirect("~/error.aspx?");
        }
         if (GridView1.SelectedValue == null || TextBox1.Text=="")
         {
             Response.Write("<script>alert('未选择要确认的货物或者未填入数量!')</script>");
         }
         else
         {
             SqlConnection myConn = GetConnection();
             myConn.Open();
             string sqlStr = "select RID , RGID ,GTYPEName ,GCHName ,GXinhao ,GBrand ,  RWID ,RQuantity ,RHandler ,RDate ,RifAOG,RComfirmer ,RComfirmedDate ,bianhao,PName ,PID  from Projects,RecordsIN, Goods where RecordsIN.RGID=Goods.GID and RecordsIN.RPID=Projects.PID and RecordsIN.RID='" + GridView1.SelectedValue.ToString() + "'";
             SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
             myCmd.ExecuteNonQuery();
             SqlDataReader read = myCmd.ExecuteReader();
             read.Read();
             //string queren = read["RQuantity"].ToString();
             string sum = TextBox1.Text;
             string pid = read["PID"].ToString();
             string pgid = read["RGID"].ToString();
             string rid = read["RID"].ToString();
             string PGName = read["GCHName"].ToString();
             read.Close();

             string logname;
             string sqllname = "select UID from Users where UName='" + Session["name"].ToString() + "'";
             SqlCommand comname = new SqlCommand(sqllname, myConn);
             comname.ExecuteNonQuery();
             SqlDataReader readname = comname.ExecuteReader();
             readname.Read();
             string UID = readname["UID"].ToString();
             readname.Close();
             logname = UID;

             string pgname;
             //string sqlpgname="select * from ProjectsQuantity where PGID='"+ pgid +"'";
             //SqlCommand compgname=new SqlCommand(sqlpgname,myConn);
             //compgname.ExecuteNonQuery();
             //SqlDataReader readpgname=compgname.ExecuteReader();
             //readpgname.Read();
             //string PGName=readpgname["PGName"].ToString();
             //readpgname.Close();
             pgname = PGName;

             if (!IsNumberic(sum))
             {
                 Response.Write("<script>alert('输入的数量有误!')</script>");
             }
             else
             {
                 string sql1 = "select * from ProjectsQuantity where PID=" + pid + " and PGID=" + pgid + "";
                 SqlCommand com2 = new SqlCommand(sql1, myConn);
                 SqlDataReader dr1 = com2.ExecuteReader();
                 if (dr1.Read())
                 {
                     DateTime dt = DateTime.Now;
                     dr1.Close();
                     string sqllll = "update  ProjectsQuantity set PQuantity=PQuantity + '" + sum + "' where PID='" + pid + "' and PGID='" + pgid + "'";
                     string sql = "update RecordsIN SET RifAOG=RifAOG+'" + sum + "',RComfirmer='" + logname + "',RComfirmedDate='" + dt + "'  where RID='" + GridView1.SelectedValue + "' ";

                     SqlCommand com = new SqlCommand(sql, myConn);
                     com.ExecuteNonQuery();
                     com.CommandText = sqllll;
                     com.ExecuteNonQuery();
                     string sqlll = "update Store set SQuantity=SQuantity+'" + sum + "' where SGID='" + pgid + "'";
                     com.CommandText = sqlll;
                     com.ExecuteNonQuery();
                     myConn.Close();
                 }
                 else
                 {
                     DateTime dt = DateTime.Now;
                     dr1.Close();
                     //string sqll1 ="select RManager from  RecordsIN where RID='" + GridView1.SelectedValue + "'";
                     //SqlCommand com3 = new SqlCommand(sqll1, myConn);
                     //object obj = com3.ExecuteScalar();
                     //string RHandler = Convert.ToString(obj);
                     string sqllll = "insert into ProjectsQuantity (PID,PGID,PGName,PQuantity) values('" + pid + "','" + pgid + "','" + pgname + "','" + sum + "')";
                     string sql = "update RecordsIN SET RifAOG=RifAOG+'"+ sum +"',RComfirmer='" + logname + "',RComfirmedDate='" + dt + "'  where RID='" + GridView1.SelectedValue + "' ";

                     SqlCommand com = new SqlCommand(sql, myConn);
                     com.ExecuteNonQuery();
                     com.CommandText = sqllll;
                     com.ExecuteNonQuery();
                     string sqlll = "update Store set SQuantity=SQuantity+'" + sum + "' where SGID='" + pgid + "'";
                     com.CommandText = sqlll;
                     com.ExecuteNonQuery();
                     Response.Write("<script>alert('操作成功!')</script>");
                     myConn.Close();
                 }
                 //Response.Redirect("~/success.aspx?");

                 Response.Write("<script>alert('操作成功!')</script>");
                 Response.AddHeader("Refresh", "0");
             }
             
         }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}